home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Programming / Programming Languages / XLisp 2.1e3 / Sources / macstuff.c < prev    next >
Text File  |  1993-11-13  |  3KB  |  111 lines

  1. /* macstuff.c - macintosh interface routines for xlisp */
  2. /* Written by Brian Kendig. */
  3. /* This file contains the stuff that the other xlisp files call directly. */
  4.  
  5. #include <stdio.h>
  6. #include <QuickDraw.h>    /* for Random */
  7. #include <Memory.h>        /* for DisposPtr */
  8. #include <SegLoad.h>    /* for ExitToShell */
  9. #include "xlisp.h"
  10.  
  11. #define DELETE 0x08
  12.  
  13. /* externals */
  14. extern FILE *tfp;  /* transcript file pointer */
  15. extern int cursorPos;
  16. extern char *macgets (void);
  17.  
  18. /* local variables */
  19. static char *linebuf = NULL, *lineptr;
  20. static int numChars;
  21. int lposition;
  22.  
  23. int isascii (char c) { return 1; }  /* every char is an ascii char, isn't it? */
  24.  
  25. void osinit (char *banner) {
  26.     int i;
  27.     char version[] = "\nMacintosh interface by Brian Kendig.\n";
  28.     InitMac ();  /* initialize the mac interface routines */
  29.     lposition = 0;  /* initialize the line editor */
  30.     for (i = 0; banner[i] != '\0'; i++) macputc (banner[i]);
  31.     for (i = 0; version[i] != '\0'; i++) macputc (version[i]);
  32. }
  33.  
  34. /* osrand - return next random number in sequence */
  35. long osrand (long rseed) {
  36.     long k1;
  37.  
  38.     /* make sure we don't get stuck at zero */
  39.     if (rseed == 0L) rseed = 1L;
  40.  
  41.     /* algorithm taken from Dr. Dobbs Journal, November 1985, page 91 */
  42.     k1 = rseed / 127773L;
  43.     if ((rseed = 16807L * (rseed - k1 * 127773L) - k1 * 2836L) < 0L)
  44.         rseed += 2147483647L;
  45.  
  46.     /* return a random number between 0 and MAXFIX */
  47.     return rseed;
  48. }
  49.  
  50. FILEP osaopen (char *name, char *mode) {
  51.     return fopen (name, mode);
  52. }
  53.  
  54. FILEP osbopen (char *name, char *mode) {
  55.     char nmode[4];
  56.     strcpy (nmode, mode); strcat (nmode, "b");
  57.     return (fopen (name, nmode));
  58. }
  59.  
  60. int osclose (FILE *fp) { return (fclose (fp)); }
  61. int osaputc (int ch, FILE *fp) { return (putc (ch, fp)); }
  62. int osbputc (int ch, FILE *fp) { return (putc (ch, fp)); }
  63.  
  64. int ostgetc (void) {
  65.     int i;
  66.  
  67.     if (numChars <= 0) {  /* get some more */
  68.         if (linebuf) DisposPtr (linebuf);
  69.         linebuf = macgets ();
  70.         i = 0;
  71.         while (linebuf[i] != '\0') i++;
  72.         numChars = i;
  73.         if (tfp) for (i = 0; i < numChars; i++) osaputc (linebuf[i], tfp);
  74.         lineptr = linebuf;
  75.     }
  76.     numChars--;
  77.     if (*lineptr == '\r') {
  78.         lineptr++;
  79.         return '\n';
  80.     } else return (*lineptr++);
  81. }
  82.  
  83. void ostputc (int ch) {
  84.     macputc (ch);
  85.     if (tfp) osaputc (ch, tfp);
  86. }
  87.  
  88. void osflush (void) {
  89.     lineptr = linebuf;
  90.     numChars = 0;
  91.     lposition = 0;
  92. }
  93.  
  94. void oscheck (void) { DoEvent (); }
  95.  
  96. void oserror (char *msg) {
  97.     char line[100], *p;
  98.     sprintf (line,"error: %s\n",msg);
  99.     for (p = line; *p != '\0'; ++p) ostputc (*p);
  100. }
  101.  
  102. void osfinish (void) {
  103.     /* dispose of everything... */
  104.     if (linebuf) DisposPtr (linebuf);
  105.     MacWrapUp ();
  106.     ExitToShell ();
  107. }
  108.  
  109. int renamebackup (char *filename) { return 0; }
  110.  
  111.